<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/10/27
  Time: 8:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>cookieSave</title>
</head>
<body>
<%
//    设置编码格式
    request.setCharacterEncoding("utf-8");
//    获取信息
    String uName = request.getParameter("userName");
    String pass = request.getParameter("pwd");
    String[] flag = request.getParameterValues("flag");
//    记住用户名
    if(flag!=null && flag.length>0){
//        新建cookie
        Cookie co1 = new Cookie("userName",uName);
        Cookie co2 = new Cookie("pwd",pass);
//        设置有效期（1天）
        co1.setMaxAge(2*60);
        co2.setMaxAge(2*60);
//        将cookie对象存放到response中
        response.addCookie(co1);
        response.addCookie(co2);
    }else{
        //    没有选择记住用户名
        Cookie[] cookies=request.getCookies();
        if (cookies!=null && cookies.length>0){
            for (Cookie cookie:cookies) {
                if (cookie.getName().equals("userName")){
                    cookie.setMaxAge(0);
                }
                if (cookie.getName().equals("pwd")){
                    cookie.setMaxAge(0);
                }
                response.addCookie(cookie);
            }
        }
    }



%>
<a href="cookieQuery.jsp">查看cookie</a>
</body>
</html>
